package middleware
import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
	)
	
	// 跨域请求
	
	func Cors() gin.HandlerFunc {
		handlerFunc := cors.New(cors.Config{
			AllowMethods:     []string{"*"},
			AllowHeaders:     []string{"content-type", "token", "fileType", "size", "digest"}, //此处设置非默认之外的请求头(自定义请求头),否则会出现跨域问题
			AllowAllOrigins:  true,
			AllowCredentials: true,
			MaxAge:           24 * time.Hour,
			ExposeHeaders:    []string{"*"},
		})
		return handlerFunc
	}
	
	// gin 上下文配置 cors
	
	func CORSMiddleware() gin.HandlerFunc {
		return func(c *gin.Context) {
			c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
			c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
			c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
			c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, HEAD, OPTIONS")
	
			if c.Request.Method == "OPTIONS" {
				c.AbortWithStatus(204)
				return
			}
	
			c.Next()
		}
	}
	
	//nginx 跨域
	
	//server {
	//#以上省略
	//add_header 'Access-Control-Allow-Origin' '*';
	//add_header 'Access-Control-Allow-Headers' 'X-Pagination-Current-Page,Content-Type';
	//add_header 'Access-Control-Allow-Methods' 'PUT,GET,POST,HEAD,DELETE';
	//add_header 'Access-Control-Expose-Headers' 'X-Pagination-Current-Page,Content-Type';
	//#以下省略
	//}
	//Allow-Headers "Accept","Accept-Encoding","Host","Origin","Referer","User-Agent",